

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/IKKIS_LOGO.png">
  <link rel="icon" href="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/IKKIS_LOGO.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Ikki">
  <meta name="keywords" content="">
  
    <meta name="description" content="JavaScript中用于表示二进制对象有，Blob对象、和 ArrayBuffer 对象。">
<meta property="og:type" content="article">
<meta property="og:title" content="二进制对象">
<meta property="og:url" content="http://81.68.132.127/2022/10/20/JS/binary/%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AF%B9%E8%B1%A1/index.html">
<meta property="og:site_name" content="IKKI">
<meta property="og:description" content="JavaScript中用于表示二进制对象有，Blob对象、和 ArrayBuffer 对象。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/beijing/iTab-572k81.png">
<meta property="article:published_time" content="2022-10-20T15:11:05.000Z">
<meta property="article:modified_time" content="2022-10-20T15:13:01.698Z">
<meta property="article:author" content="Ikki">
<meta property="article:tag" content="前端基础">
<meta property="article:tag" content="js">
<meta property="article:tag" content="二进制对象">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/beijing/iTab-572k81.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>二进制对象 - IKKI</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="/fluid/css/fluid-extention.css">
<link rel="stylesheet" href="/fluid/css/nihao.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"81.68.132.127","root":"/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":3},"lazyload":{"enable":true,"loading_img":"https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 100vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Ikki-BUG制造者</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('http://image.ikki0603.cn') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="二进制对象"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-10-20 23:11" pubdate>
          2022年10月20日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          13k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          110 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
        <div class="scroll-down-bar">
          <i class="iconfont icon-arrowdown"></i>
        </div>
      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">二进制对象</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2022年10月20日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h2 id="1-生成项目"><a href="#1-生成项目" class="headerlink" title="1.生成项目 #"></a>1.生成项目 <a href="#t01.%E7%94%9F%E6%88%90%E9%A1%B9%E7%9B%AE">#</a></h2><figure class="highlight dsconfig"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-nsf0rqlacl5vpm"></i><span>dsconfig</span><div class="collapse show" id="collapse-nsf0rqlacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs dsconfig"><span class="hljs-built_in">create-react-app</span> <span class="hljs-string">project-binady-object</span><br><span class="hljs-string">cd</span> <span class="hljs-string">project-binady-object</span><br><span class="hljs-string">yarn</span> <span class="hljs-string">start</span><br><br></code></pre></td></tr></table></div></figure>

<p><img src="http://img.zhufengpeixun.cn/browserbinary2.jpg" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="browserbinary2"></p>
<h2 id="2-ArrayBuffer"><a href="#2-ArrayBuffer" class="headerlink" title="2.ArrayBuffer #"></a>2.ArrayBuffer <a href="#t12.ArrayBuffer">#</a></h2><ul>
<li>  <a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</a>对象用来表示通用的、固定长度的原始二进制数据缓冲区</li>
<li>  它是一个字节数组，通常在其他语言中称为<code>byte array</code></li>
<li>  你不能直接操作 <code>ArrayBuffer</code> 的内容，而是要通过<code>类型数组对象</code>或 <code>DataView</code> 对象来操作，它们会将缓冲区中的数据表示为特定的格式，并通过这些格式来读写缓冲区的内容。</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/bytearray.jpg" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="bytearray"></p>
<figure class="highlight arcade"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-0ldyfylacl5vpm"></i><span>arcade</span><div class="collapse show" id="collapse-0ldyfylacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs arcade"><span class="hljs-comment">//创建一个长度为8个字节的buffer</span><br>const <span class="hljs-built_in">buffer</span> = <span class="hljs-keyword">new</span> ArrayBuffer(<span class="hljs-number">8</span>);<br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-built_in">buffer</span>.byteLength);<br><br></code></pre></td></tr></table></div></figure>

<h2 id="3-TypedArray"><a href="#3-TypedArray" class="headerlink" title="3.TypedArray #"></a>3.TypedArray <a href="#t23.TypedArray">#</a></h2><p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">TypedArray</a>对象描述了一个底层的二进制数据缓冲区(binary data buffer)的一个类数组视图(view)</p>
<ul>
<li>  但它本身不可以被实例化，甚至无法访问，你可以把它理解为接口,它有很多的实现</li>
</ul>
<table>
<thead>
<tr>
<th>类型</th>
<th>单个元素值的范围</th>
<th>大小(bytes)</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>Int8Array</td>
<td>-128 to 127</td>
<td>1</td>
<td>8 位二进制有符号整数</td>
</tr>
<tr>
<td>Uint8Array</td>
<td>0 to 255</td>
<td>1</td>
<td>8 位无符号整数</td>
</tr>
<tr>
<td>Int16Array</td>
<td>-32768 to 32767</td>
<td>2</td>
<td>16 位二进制有符号整数</td>
</tr>
<tr>
<td>Uint16Array</td>
<td>0 to 65535</td>
<td>2</td>
<td>16 位无符号整数</td>
</tr>
</tbody></table>
<p><img src="http://img.zhufengpeixun.cn/TypedArray.jpg" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="TypedArray"></p>
<figure class="highlight arcade"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-sllrhclacl5vpm"></i><span>arcade</span><div class="collapse show" id="collapse-sllrhclacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs arcade">const <span class="hljs-built_in">buffer</span> = <span class="hljs-keyword">new</span> ArrayBuffer(<span class="hljs-number">8</span>);<br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-built_in">buffer</span>.byteLength);<span class="hljs-comment">//8</span><br>const int8Array = <span class="hljs-keyword">new</span> Int8Array(<span class="hljs-built_in">buffer</span>);<br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(int8Array.<span class="hljs-built_in">length</span>);<span class="hljs-comment">//8</span><br>const int16Array = <span class="hljs-keyword">new</span> Int16Array(<span class="hljs-built_in">buffer</span>);<br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(int16Array.<span class="hljs-built_in">length</span>);<span class="hljs-comment">//4</span><br><br></code></pre></td></tr></table></div></figure>

<h2 id="4-DataView对象"><a href="#4-DataView对象" class="headerlink" title="4.DataView对象 #"></a>4.DataView对象 <a href="#t34.DataView%E5%AF%B9%E8%B1%A1">#</a></h2><ul>
<li>  <a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/DataView">DataView</a>视图是一个可以从二进制ArrayBuffer对象中读写多种数值类型的底层接口</li>
<li>  <code>setInt8()</code>从DataView起始位置以byte为计数的指定偏移量(byteOffset)处储存一个8-bit数(一个字节)</li>
<li>  <code>getInt8()</code>从DataView起始位置以byte为计数的指定偏移量(byteOffset)处获取一个8-bit数(一个字节)</li>
</ul>
<figure class="highlight reasonml"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-x6ih2ylacl5vpm"></i><span>reasonml</span><div class="collapse show" id="collapse-x6ih2ylacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs reasonml"><span class="hljs-keyword">new</span> <span class="hljs-constructor">DataView(<span class="hljs-params">buffer</span> [, <span class="hljs-params">byteOffset</span> [, <span class="hljs-params">byteLength</span>]])</span><br><br></code></pre></td></tr></table></div></figure>

<p><img src="http://img.zhufengpeixun.cn/DataView.jpg" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload alt="DataView"></p>
<figure class="highlight arcade"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-vvsqw9lacl5vpm"></i><span>arcade</span><div class="collapse show" id="collapse-vvsqw9lacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs arcade">const <span class="hljs-built_in">buffer</span> = <span class="hljs-keyword">new</span> ArrayBuffer(<span class="hljs-number">8</span>);<br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-built_in">buffer</span>.byteLength);<span class="hljs-comment">//8个字节</span><br>const view1 = <span class="hljs-keyword">new</span> DataView(<span class="hljs-built_in">buffer</span>);<br>view1.setInt8(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>); <br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(view1.getInt8(<span class="hljs-number">0</span>));<span class="hljs-comment">//1</span><br><br>view1.setInt8(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(view1.getInt8(<span class="hljs-number">1</span>));<span class="hljs-comment">//2</span><br><br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(view1.getInt16(<span class="hljs-number">0</span>));<span class="hljs-comment">//258</span><br><br></code></pre></td></tr></table></div></figure>

<h2 id="5-Blob"><a href="#5-Blob" class="headerlink" title="5.Blob #"></a>5.Blob <a href="#t45.Blob">#</a></h2><ul>
<li>  <a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/Blob">Blob</a>对象表示一个不可变、原始数据的类文件对象。Blob 表示的不一定是JavaScript原生格式的数据。File 接口基于Blob，继承了 blob 的功能并将其扩展使其支持用户系统上的文件。</li>
<li>  <a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/Blob/Blob">Blob()</a> 构造函数返回一个新的 Blob 对象。 blob的内容由参数数组中给出的值的串联组成。</li>
<li><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader">FileReader</a> 对象允许Web应用程序异步读取存储在用户计算机上的文件（或原始数据缓冲区）的内容，使用 File 或 Blob 对象指定要读取的文件或数据。<ul>
<li>  <code>readAsText()</code>：读取文本文件（可以使用txt打开的文件），返回文本字符串，默认编码是UTF-8</li>
<li>  <code>readAsDataURL()</code>：读取文件获取一段以data开头的字符串，这段字符串的本质就是DataURL，DataURL是一种将文件嵌入到文档的方案。DataURL是将资源转换为base64编码的字符串形式,并且将这些内容直接储存在url中</li>
</ul>
</li>
<li>构造函数 <code>var aBlob = new Blob( array, options );</code><ul>
<li>  array 是一个由<code>ArrayBuffer</code>, <code>ArrayBufferView</code>, <code>Blob</code>, <code>DOMString</code> 等对象构成的 Array ，或者其他类似对象的混合体，它将会被放进 Blob。DOMStrings会被编码为UTF-8。</li>
<li>options 是一个可选的<code>BlobPropertyBag</code>字典<ul>
<li>  <code>type</code> 默认值为 “”,它代表了将会被放入到blob中的数组内容的MIME类型</li>
</ul>
</li>
</ul>
</li>
</ul>
<figure class="highlight xml"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-b5w43dlacl5vpm"></i><span>xml</span><div class="collapse show" id="collapse-b5w43dlacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">     <span class="hljs-keyword">let</span> debug = &#123; <span class="hljs-attr">name</span>: <span class="hljs-string">&quot;zhufeng&quot;</span> &#125;;</span><br><span class="language-javascript">     <span class="hljs-keyword">let</span> str = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(debug);</span><br><span class="language-javascript">     <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;str&quot;</span>, str);</span><br><span class="language-javascript"></span><br><span class="language-javascript">     <span class="hljs-keyword">var</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([str], &#123; <span class="hljs-attr">type</span>: <span class="hljs-string">&quot;application/json&quot;</span> &#125;);</span><br><span class="language-javascript">     <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;blob.size&quot;</span>, blob.<span class="hljs-property">size</span>);</span><br><span class="language-javascript"></span><br><span class="language-javascript">        <span class="hljs-keyword">function</span> <span class="hljs-title function_">readBlob</span>(<span class="hljs-params">blob, type</span>) &#123;</span><br><span class="language-javascript">            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> &#123;</span><br><span class="language-javascript">                <span class="hljs-keyword">const</span> reader = <span class="hljs-keyword">new</span> <span class="hljs-title class_">FileReader</span>();</span><br><span class="language-javascript">                reader.<span class="hljs-property">onload</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) &#123;</span><br><span class="language-javascript">                    <span class="hljs-title function_">resolve</span>(event.<span class="hljs-property">target</span>.<span class="hljs-property">result</span>);</span><br><span class="language-javascript">                &#125;;</span><br><span class="language-javascript">                <span class="hljs-keyword">switch</span> (type) &#123;</span><br><span class="language-javascript">                    <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;ArrayBuffer&quot;</span>:</span><br><span class="language-javascript">                        reader.<span class="hljs-title function_">readAsArrayBuffer</span>(blob);</span><br><span class="language-javascript">                        <span class="hljs-keyword">break</span>;</span><br><span class="language-javascript">                    <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;DataURL&quot;</span>:</span><br><span class="language-javascript">                        reader.<span class="hljs-title function_">readAsDataURL</span>(blob);</span><br><span class="language-javascript">                        <span class="hljs-keyword">break</span>;</span><br><span class="language-javascript">                    <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;Text&quot;</span>:</span><br><span class="language-javascript">                        reader.<span class="hljs-title function_">readAsText</span>(blob,<span class="hljs-string">&#x27;UTF-8&#x27;</span>);</span><br><span class="language-javascript">                        <span class="hljs-keyword">break</span>;</span><br><span class="language-javascript">                    <span class="hljs-attr">default</span>:</span><br><span class="language-javascript">                        <span class="hljs-keyword">break</span>;</span><br><span class="language-javascript">                &#125;</span><br><span class="language-javascript">            &#125;);</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">        <span class="hljs-title function_">readBlob</span>(blob, <span class="hljs-string">&quot;ArrayBuffer&quot;</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">buffer</span>) =&gt;</span> &#123;</span><br><span class="language-javascript">            <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;buffer&quot;</span>, buffer);</span><br><span class="language-javascript">        &#125;);</span><br><span class="language-javascript">        <span class="hljs-title function_">readBlob</span>(blob, <span class="hljs-string">&quot;DataURL&quot;</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">base64String</span>) =&gt;</span> &#123;</span><br><span class="language-javascript">            <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;base64String&quot;</span>, base64String);</span><br><span class="language-javascript">        &#125;);</span><br><span class="language-javascript">        <span class="hljs-title function_">readBlob</span>(blob, <span class="hljs-string">&quot;Text&quot;</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">text</span>) =&gt;</span> &#123;</span><br><span class="language-javascript">            <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;text&quot;</span>, text);</span><br><span class="language-javascript">        &#125;);</span><br><span class="language-javascript">    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><br></code></pre></td></tr></table></div></figure>

<h2 id="6-Object-URL-t56-Object-URL"><a href="#6-Object-URL-t56-Object-URL" class="headerlink" title="6. Object URL [#](#t56. Object URL)"></a>6. Object URL [#](#t56. Object URL)</h2><ul>
<li>  可以使用浏览器新的 API URL 对象通过方法生成一个地址来表示 <code>Blob</code> 数据</li>
<li>  格式为 <code>blob:&lt;origin&gt;/&lt;uuid&gt;</code></li>
<li>  <a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/URL/createObjectURL">URL.createObjectURL</a> 静态方法会创建一个 DOMString，其中包含一个表示参数中给出的对象的URL。这个 URL 的生命周期和创建它的窗口中的 document 绑定。这个新的URL 对象表示指定的 File 对象或 Blob 对象。</li>
<li>  <a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/URL/revokeObjectURL">revokeObjectURL</a> 静态方法用来释放一个之前已经存在的、通过调用 <code>URL.createObjectURL()</code> 创建的 URL 对象</li>
</ul>
<figure class="highlight xml"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-gpw1yzlacl5vpm"></i><span>xml</span><div class="collapse show" id="collapse-gpw1yzlacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;download()&quot;</span>&gt;</span>下载json<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">     <span class="hljs-keyword">function</span> <span class="hljs-title function_">download</span> () &#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">let</span> debug = &#123; <span class="hljs-attr">hello</span>: <span class="hljs-string">&quot;world&quot;</span> &#125;;</span><br><span class="language-javascript">        <span class="hljs-keyword">let</span> str = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(debug);</span><br><span class="language-javascript">        <span class="hljs-keyword">var</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([str], &#123; <span class="hljs-attr">type</span>: <span class="hljs-string">&#x27;application/json&#x27;</span> &#125;);</span><br><span class="language-javascript">        <span class="hljs-keyword">let</span> objectURL = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">createObjectURL</span>(blob);</span><br><span class="language-javascript">        <span class="hljs-keyword">const</span> a = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(<span class="hljs-string">&#x27;a&#x27;</span>);</span><br><span class="language-javascript">        a.<span class="hljs-property">download</span> = <span class="hljs-string">&#x27;hello.json&#x27;</span>;</span><br><span class="language-javascript">        a.<span class="hljs-property">rel</span> = <span class="hljs-string">&#x27;noopener&#x27;</span>;</span><br><span class="language-javascript">        a.<span class="hljs-property">href</span> = objectURL;</span><br><span class="language-javascript">        a.<span class="hljs-title function_">dispatchEvent</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">MouseEvent</span>(<span class="hljs-string">&#x27;click&#x27;</span>));</span><br><span class="language-javascript">        <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">revokeObjectURL</span>(objectURL);</span><br><span class="language-javascript">     &#125;</span><br><span class="language-javascript">    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><br></code></pre></td></tr></table></div></figure>

<h2 id="7-图片预览和裁剪上传"><a href="#7-图片预览和裁剪上传" class="headerlink" title="7.图片预览和裁剪上传 #"></a>7.图片预览和裁剪上传 <a href="#t67.%E5%9B%BE%E7%89%87%E9%A2%84%E8%A7%88%E5%92%8C%E8%A3%81%E5%89%AA%E4%B8%8A%E4%BC%A0">#</a></h2><ul>
<li>  <a target="_blank" rel="noopener" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">bootstrap.min.css</a></li>
</ul>
<h3 id="7-1-src-index-js-t77-1-src-index-js"><a href="#7-1-src-index-js-t77-1-src-index-js" class="headerlink" title="7.1 src\index.js [#](#t77.1 src\index.js)"></a>7.1 src\index.js [#](#t77.1 src\index.js)</h3><figure class="highlight javascript"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-iulna3lacl5vpm"></i><span>javascript</span><div class="collapse show" id="collapse-iulna3lacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">React</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;react&#x27;</span>;<br><span class="hljs-keyword">import</span> <span class="hljs-title class_">ReactDOM</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;react-dom&#x27;</span>;<br><span class="hljs-keyword">import</span> <span class="hljs-title class_">Image</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./Image&#x27;</span>;<br><br><span class="hljs-title class_">ReactDOM</span>.<span class="hljs-title function_">render</span>(<br>  <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">Image</span> /&gt;</span></span>,<br>  <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;root&#x27;</span>)<br>);<br><br></code></pre></td></tr></table></div></figure>

<h3 id="7-2-src-Image-js-t87-2-src-Image-js"><a href="#7-2-src-Image-js-t87-2-src-Image-js" class="headerlink" title="7.2 src\Image.js [#](#t87.2 src\Image.js)"></a>7.2 src\Image.js [#](#t87.2 src\Image.js)</h3><p>src\Image.js</p>
<figure class="highlight kotlin"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-ar16a7lacl5vpm"></i><span>kotlin</span><div class="collapse show" id="collapse-ar16a7lacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br></pre></td><td class="code"><pre><code class="hljs kotlin"><span class="hljs-keyword">import</span> React from <span class="hljs-string">&#x27;react&#x27;</span>;<br>export default <span class="hljs-keyword">class</span> <span class="hljs-title class_">Image</span> <span class="hljs-title">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span>&#123;<br>   imageRef = React.createRef()   <span class="hljs-comment">//图片</span><br>   canvasRef = React.createRef()  <span class="hljs-comment">//完整的canvas</span><br>   avatarRef = React.createRef()  <span class="hljs-comment">//裁剪后的头像</span><br>   state = &#123;<br>    file:<span class="hljs-literal">null</span>,<span class="hljs-comment">//添加的文件</span><br>    dataURL:<span class="hljs-string">&#x27;&#x27;</span>,<span class="hljs-comment">//转换成的base64字符串</span><br>    times:<span class="hljs-number">1</span>,<span class="hljs-comment">//图片放大的倍数</span><br>    lastLeft:<span class="hljs-number">0</span>,<span class="hljs-comment">//最后一个左边的距离</span><br>    lastTop:<span class="hljs-number">0</span>,<span class="hljs-comment">//最后一个上面的距离</span><br>    avatarDataURL:<span class="hljs-string">&#x27;&#x27;</span><span class="hljs-comment">//头像的base64字符串</span><br>   &#125;<br>   handleChange = (event)=&gt;&#123;<span class="hljs-comment">//头像选择改变</span><br>    let file = event.target.files[<span class="hljs-number">0</span>];<br>    let fileReader = new FileReader();<span class="hljs-comment">//读取文件</span><br>    fileReader.onload =  (event)=&gt; &#123;<br>      <span class="hljs-keyword">this</span>.setState(&#123;file,dataURL:event.target.result&#125;);<br>      <span class="hljs-keyword">this</span>.imageRef.current.onload = ()=&gt;<span class="hljs-keyword">this</span>.draw();<span class="hljs-comment">//绘制到canvas上</span><br>    &#125;;<br>    fileReader.readAsDataURL(file);<br>   &#125;<br>   draw=(left=<span class="hljs-keyword">this</span>.state.lastLeft,top=<span class="hljs-keyword">this</span>.state.lastTop)=&gt;&#123;<br>    let image = <span class="hljs-keyword">this</span>.imageRef.current;<span class="hljs-comment">//图像</span><br>    let canvas = <span class="hljs-keyword">this</span>.canvasRef.current;<span class="hljs-comment">//canvas</span><br>    <span class="hljs-keyword">const</span> ctx = canvas.getContext(<span class="hljs-string">&quot;2d&quot;</span>);<br>    ctx.clearRect(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,canvas.width, canvas.height);<span class="hljs-comment">//清掉老图片</span><br>    let imageWidth = image.width;<span class="hljs-comment">//图片宽度</span><br>    let imageHeight = image.height;<span class="hljs-comment">//图片高度</span><br>    <span class="hljs-keyword">if</span>(imageWidth&gt;imageHeight)&#123;<span class="hljs-comment">//如果宽比高度大</span><br>      let times = canvas.width/imageWidth;<br>      imageWidth=canvas.width*<span class="hljs-keyword">this</span>.state.times;<span class="hljs-comment">//让宽度等于canvas宽度</span><br>      imageHeight=times*imageHeight*<span class="hljs-keyword">this</span>.state.times;<span class="hljs-comment">//然后让高度等比缩放</span><br>    &#125;<span class="hljs-keyword">else</span>&#123;<br>      let times = canvas.height/imageHeight;<br>      imageHeight=canvas.height*<span class="hljs-keyword">this</span>.state.times;<br>      imageWidth=times*imageWidth*<span class="hljs-keyword">this</span>.state.times;<br>    &#125;<br>    ctx.drawImage(image, (canvas.width-imageWidth)/<span class="hljs-number">2</span>+left, (canvas.height-imageHeight)/<span class="hljs-number">2</span>+top,imageWidth,imageHeight);<br>   &#125;<br>   bigger = ()=&gt;&#123;<br>       <span class="hljs-keyword">this</span>.setState(&#123;times:<span class="hljs-keyword">this</span>.state.times+<span class="hljs-number">0.1</span>&#125;,()=&gt;<span class="hljs-keyword">this</span>.draw());<br>   &#125;<br>   smaller = ()=&gt;&#123;<br>    <span class="hljs-keyword">this</span>.setState(&#123;times:<span class="hljs-keyword">this</span>.state.times-<span class="hljs-number">0.1</span>&#125;,()=&gt;<span class="hljs-keyword">this</span>.draw());<br>   &#125;<br>   confirm = ()=&gt;&#123;<br>    let canvas = <span class="hljs-keyword">this</span>.canvasRef.current;<span class="hljs-comment">//头像canvas</span><br>    <span class="hljs-keyword">const</span> ctx = canvas.getContext(<span class="hljs-string">&quot;2d&quot;</span>);<br>    <span class="hljs-keyword">const</span> imageData = ctx.getImageData(<span class="hljs-number">100</span>, <span class="hljs-number">100</span>, <span class="hljs-number">100</span>, <span class="hljs-number">100</span>);<span class="hljs-comment">//获取头像数据</span><br>    let clipCanvas = document.createElement(<span class="hljs-string">&#x27;canvas&#x27;</span>);<br>    clipCanvas.width=<span class="hljs-number">100</span>;<br>    clipCanvas.height=<span class="hljs-number">100</span>;<br>    <span class="hljs-keyword">const</span> clipContext = clipCanvas.getContext(<span class="hljs-string">&quot;2d&quot;</span>);<br>    clipContext.putImageData(imageData, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>);<br>    let dataUrl = clipCanvas.toDataURL();<br>    <span class="hljs-keyword">this</span>.avatarRef.current.src = dataUrl;<br>    <span class="hljs-keyword">this</span>.setState(&#123;avatarDataURL:dataUrl&#125;);<br>   &#125;<br>   handleMouseDown = (event)=&gt;&#123;<br>    <span class="hljs-keyword">this</span>.setState(&#123;startX:event.clientX,startY:event.clientY,startDrag:<span class="hljs-literal">true</span>&#125;);<br>   &#125;<br>   handleMouseMove = (event)=&gt;&#123;<br>    <span class="hljs-keyword">if</span>(<span class="hljs-keyword">this</span>.state.startDrag)<br>        <span class="hljs-keyword">this</span>.draw((event.clientX - <span class="hljs-keyword">this</span>.state.startX)+<span class="hljs-keyword">this</span>.state.lastLeft,(event.clientY - <span class="hljs-keyword">this</span>.state.startY)+<span class="hljs-keyword">this</span>.state.lastTop);<br>   &#125;<br>   handleMouseUp = (event)=&gt;&#123;<br>    <span class="hljs-keyword">this</span>.setState(&#123;lastLeft:(event.clientX - <span class="hljs-keyword">this</span>.state.startX)+<span class="hljs-keyword">this</span>.state.lastLeft,lastTop:(event.clientY - <span class="hljs-keyword">this</span>.state.startY)+<span class="hljs-keyword">this</span>.state.lastTop,startDrag:<span class="hljs-literal">false</span>&#125;);<br>   &#125;<br>   upload = ()=&gt;&#123;<br>    let bytes = atob(<span class="hljs-keyword">this</span>.state.avatarDataURL.split(<span class="hljs-string">&quot;,&quot;</span>)[<span class="hljs-number">1</span>]);<br>    let arrayBuffer = new ArrayBuffer(bytes.length);<br>    let uint8Array = new Uint8Array(arrayBuffer);<br>    <span class="hljs-keyword">for</span> (let i = <span class="hljs-number">0</span>; i &lt; bytes.length; i++) &#123;<br>      uint8Array[i] = bytes.charCodeAt(i);<br>    &#125;<br>    let blob =  new Blob([arrayBuffer], &#123; type: <span class="hljs-string">&#x27;image/png&#x27;</span> &#125;);<br>    let request = new XMLHttpRequest();<br>    let formData = new FormData();<br>    formData.append(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;zhufeng&quot;</span>);<br>    formData.append(<span class="hljs-string">&quot;avatar&quot;</span>, blob);<br>    request.<span class="hljs-keyword">open</span>(<span class="hljs-string">&quot;POST&quot;</span>, <span class="hljs-string">&#x27;http://localhost:8080/upload&#x27;</span>, <span class="hljs-literal">true</span>);<br>    request.send(formData);<br>   &#125;<br>   render()&#123;<br>       <span class="hljs-keyword">return</span> (<br>           &lt;div className=<span class="hljs-string">&quot;container&quot;</span>&gt;<br>               &lt;div className=<span class="hljs-string">&quot;row&quot;</span>&gt;<br>                   &lt;div className=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;<br>                     &lt;input type=<span class="hljs-string">&quot;file&quot;</span> accept=<span class="hljs-string">&quot;image/*&quot;</span> onChange=&#123;<span class="hljs-keyword">this</span>.handleChange&#125; /&gt;<br>                   &lt;/div&gt;<br>               &lt;/div&gt;<br>               &lt;div className=<span class="hljs-string">&quot;row&quot;</span>&gt;<br>                   &lt;div className=<span class="hljs-string">&quot;col-md-4 image&quot;</span> &gt;<br>                       &#123;<br>                           <span class="hljs-keyword">this</span>.state.file&amp;&amp;(<br>                            &lt;img className=<span class="hljs-string">&quot;img-responsive&quot;</span> src=&#123;<span class="hljs-keyword">this</span>.state.dataURL&#125; ref=&#123;<span class="hljs-keyword">this</span>.imageRef&#125; style=&#123;&#123;border:<span class="hljs-string">&#x27;2px dashed green&#x27;</span>&#125;&#125;/&gt;<br>                           )<br>                       &#125;<br>                   &lt;/div&gt;<br>                   &lt;div className=<span class="hljs-string">&quot;col-md-4 clip&quot;</span> onMouseDown=&#123;<span class="hljs-keyword">this</span>.handleMouseDown&#125; onMouseMove=&#123;<span class="hljs-keyword">this</span>.handleMouseMove&#125; onMouseUp=&#123;<span class="hljs-keyword">this</span>.handleMouseUp&#125;&gt;<br>                       &#123;<br>                           <span class="hljs-keyword">this</span>.state.file&amp;&amp;(<br>                            &lt;&gt;<br>                                &lt;div style=&#123;&#123;position:<span class="hljs-string">&#x27;relative&#x27;</span>&#125;&#125;&gt;<br>                                  &lt;canvas ref=&#123;<span class="hljs-keyword">this</span>.canvasRef&#125; width=<span class="hljs-string">&quot;300px&quot;</span> height=<span class="hljs-string">&quot;300px&quot;</span>  style=&#123;&#123;border:<span class="hljs-string">&#x27;2px dashed blue&#x27;</span>&#125;&#125;&gt;&lt;/canvas&gt;<br>                                  &lt;div style=&#123;&#123;width:<span class="hljs-number">100</span>,height:<span class="hljs-number">100</span>,backgroundColor:<span class="hljs-string">&#x27;yellow&#x27;</span>,opacity:<span class="hljs-number">.3</span>,position:<span class="hljs-string">&#x27;absolute&#x27;</span>,left:<span class="hljs-number">100</span>,top:<span class="hljs-number">100</span>&#125;&#125;&gt;&lt;/div&gt;<br>                                &lt;/div&gt;<br>                                &lt;div className=<span class="hljs-string">&quot;btn-group&quot;</span> role=<span class="hljs-string">&quot;group&quot;</span> &gt;<br>                                  &lt;button type=<span class="hljs-string">&quot;button&quot;</span> className=<span class="hljs-string">&quot;btn btn-default&quot;</span> onClick=&#123;()=&gt;<span class="hljs-keyword">this</span>.bigger()&#125;&gt;放大&lt;/button&gt;<br>                                  &lt;button type=<span class="hljs-string">&quot;button&quot;</span> className=<span class="hljs-string">&quot;btn btn-default&quot;</span> onClick=&#123;()=&gt;<span class="hljs-keyword">this</span>.smaller()&#125;&gt;缩小&lt;/button&gt;<br>                                  &lt;button type=<span class="hljs-string">&quot;button&quot;</span> className=<span class="hljs-string">&quot;btn btn-default&quot;</span> onClick=&#123;()=&gt;<span class="hljs-keyword">this</span>.confirm()&#125;&gt;确定&lt;/button&gt;<br>                                &lt;/div&gt;<br>                            &lt;/&gt;<br>                           )<br>                       &#125;<br>                   &lt;/div&gt;<br>                   &lt;div className=<span class="hljs-string">&quot;col-md-4 avatar&quot;</span> &gt;<br>                       &#123;<br>                           <span class="hljs-keyword">this</span>.state.file&amp;&amp;(<br>                            &lt;&gt;<br>                             &lt;img  ref=&#123;<span class="hljs-keyword">this</span>.avatarRef&#125; style=&#123;&#123;border:<span class="hljs-string">&#x27;2px dashed pink&#x27;</span>,width:<span class="hljs-string">&#x27;100px&#x27;</span>,height:<span class="hljs-string">&#x27;100px&#x27;</span>&#125;&#125;/&gt;<br>                             &lt;p&gt; &lt;button className=<span class="hljs-string">&quot;btn btn-default&quot;</span> onClick=&#123;<span class="hljs-keyword">this</span>.upload&#125;&gt;上传&lt;/button&gt;&lt;/p&gt;<br>                            &lt;/&gt;<br>                           )<br>                       &#125;<br>                   &lt;/div&gt;<br>               &lt;/div&gt;  <br>           &lt;/div&gt;<br>       )<br>   &#125;   <br>&#125;<br><br></code></pre></td></tr></table></div></figure>

<h3 id="7-3-server-js-t97-3-server-js"><a href="#7-3-server-js-t97-3-server-js" class="headerlink" title="7.3 server.js [#](#t97.3 server.js)"></a>7.3 server.js [#](#t97.3 server.js)</h3><p>server.js</p>
<figure class="highlight zephir"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-ht8cnclacl5vpm"></i><span>zephir</span><div class="collapse show" id="collapse-ht8cnclacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs zephir"><span class="hljs-keyword">let</span> express = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>);<br><span class="hljs-keyword">let</span> path = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;path&#x27;</span>);<br><span class="hljs-keyword">let</span> cors = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;cors&#x27;</span>);<br><span class="hljs-keyword">let</span> app = express();<br>app.<span class="hljs-keyword">use</span>(cors());<br>app.<span class="hljs-keyword">use</span>(express.<span class="hljs-keyword">static</span>(path.join(__dirname,<span class="hljs-string">&#x27;public&#x27;</span>)));<br><span class="hljs-keyword">const</span> multer = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;multer&#x27;</span>);<br>app.<span class="hljs-keyword">use</span>(multer(&#123;dest: <span class="hljs-string">&#x27;./uploads&#x27;</span>&#125;).single(<span class="hljs-string">&#x27;avatar&#x27;</span>));<br>app.post(<span class="hljs-string">&#x27;/upload&#x27;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(req, res)</span></span>&#123;<br>    res.json(&#123;success:<span class="hljs-keyword">true</span>&#125;);<br>&#125;);<br>app.listen(<span class="hljs-number">8080</span>,()=&gt;&#123;<br>    console.log(<span class="hljs-string">&#x27;server started at port 8080&#x27;</span>);<br>&#125;);<br><br></code></pre></td></tr></table></div></figure>

<h2 id="8-音频的裁剪和预览"><a href="#8-音频的裁剪和预览" class="headerlink" title="8.音频的裁剪和预览 #"></a>8.音频的裁剪和预览 <a href="#t108.%E9%9F%B3%E9%A2%91%E7%9A%84%E8%A3%81%E5%89%AA%E5%92%8C%E9%A2%84%E8%A7%88">#</a></h2><ul>
<li>  <a target="_blank" rel="noopener" href="https://github.com/Kagami/ffmpeg.js">ffmpeg</a></li>
<li>  <a target="_blank" rel="noopener" href="http://img.zhufengpeixun.cn/song.mp3">song</a></li>
<li>  <a target="_blank" rel="noopener" href="http://img.zhufengpeixun.cn/ffmpeg-worker-mp4.js">ffmpeg-worker-mp4.js</a></li>
</ul>
<h3 id="8-1-index-js-t118-1-index-js"><a href="#8-1-index-js-t118-1-index-js" class="headerlink" title="8.1 index.js [#](#t118.1 index.js)"></a>8.1 index.js [#](#t118.1 index.js)</h3><figure class="highlight javascript"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-1lqouilacl5vpm"></i><span>javascript</span><div class="collapse show" id="collapse-1lqouilacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">React</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;react&#x27;</span>;<br><span class="hljs-keyword">import</span> <span class="hljs-title class_">ReactDOM</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;react-dom&#x27;</span>;<br><span class="hljs-keyword">import</span> <span class="hljs-title class_">Audio</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./Audio&#x27;</span>;<br><br><span class="hljs-title class_">ReactDOM</span>.<span class="hljs-title function_">render</span>(<br>  <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">Audio</span> /&gt;</span></span>,<br>  <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;root&#x27;</span>)<br>);<br><br><br></code></pre></td></tr></table></div></figure>

<h3 id="8-2-src-Audio-js-t128-2-src-Audio-js"><a href="#8-2-src-Audio-js-t128-2-src-Audio-js" class="headerlink" title="8.2 src\Audio.js [#](#t128.2 src\Audio.js)"></a>8.2 src\Audio.js [#](#t128.2 src\Audio.js)</h3><p>src\Audio.js</p>
<figure class="highlight javascript"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-13trazlacl5vpm"></i><span>javascript</span><div class="collapse show" id="collapse-13trazlacl5vpm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">React</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;react&#x27;</span>;<br><span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;axios&#x27;</span>;<br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Audio</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">React.Component</span> &#123;<br>  audioRef = <span class="hljs-title class_">React</span>.<span class="hljs-title function_">createRef</span>()<br>  audioClipRef = <span class="hljs-title class_">React</span>.<span class="hljs-title function_">createRef</span>()<br>  startRef = <span class="hljs-title class_">React</span>.<span class="hljs-title function_">createRef</span>()<br>  endRef = <span class="hljs-title class_">React</span>.<span class="hljs-title function_">createRef</span>()<br>  clip = <span class="hljs-keyword">async</span> () =&gt; &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">worker</span> = <span class="hljs-title function_">createWorker</span>(<span class="hljs-string">&#x27;/ffmpeg-worker-mp4.js&#x27;</span>);<br>    <span class="hljs-keyword">let</span> response = <span class="hljs-keyword">await</span> <span class="hljs-title function_">axios</span>(&#123; <span class="hljs-attr">url</span>: <span class="hljs-string">&#x27;/song.mp3&#x27;</span>, <span class="hljs-attr">method</span>: <span class="hljs-string">&#x27;get&#x27;</span>, <span class="hljs-attr">responseType</span>: <span class="hljs-string">&#x27;arraybuffer&#x27;</span> &#125;);<br>    <span class="hljs-keyword">let</span> originArrayBuffer = response.<span class="hljs-property">data</span>;<br>    <span class="hljs-keyword">let</span> start = <span class="hljs-built_in">parseInt</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">startRef</span>.<span class="hljs-property">current</span>.<span class="hljs-property">value</span>);<br>    <span class="hljs-keyword">let</span> end = <span class="hljs-built_in">parseInt</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">endRef</span>.<span class="hljs-property">current</span>.<span class="hljs-property">value</span>);<br>    <span class="hljs-keyword">let</span> duration = end - start;<br>    <span class="hljs-keyword">let</span> resultArrayBuffer = (<span class="hljs-keyword">await</span> <span class="hljs-title function_">toPromise</span>(<br>      <span class="hljs-variable language_">this</span>.<span class="hljs-property">worker</span>,<br>      <span class="hljs-title function_">getClipCommand</span>(originArrayBuffer, start, duration)<br>    )).<span class="hljs-property">data</span>.<span class="hljs-property">data</span>.<span class="hljs-property">MEMFS</span>[<span class="hljs-number">0</span>].<span class="hljs-property">data</span>;<br><br>    <span class="hljs-keyword">let</span> clipBlob = <span class="hljs-title function_">audioBufferToBlob</span>(resultArrayBuffer);<br>    <span class="hljs-keyword">let</span> audio= <span class="hljs-variable language_">this</span>.<span class="hljs-property">audioClipRef</span>.<span class="hljs-property">current</span> <br>    audio.<span class="hljs-property">src</span> = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">createObjectURL</span>(clipBlob);<br>    audio.<span class="hljs-title function_">load</span>();<br>    audio.<span class="hljs-title function_">play</span>();<br>  &#125;;<br>  <span class="hljs-title function_">render</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-keyword">return</span> (<br>      <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span></span><br><span class="language-xml">        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&#123;this.startRef&#125;</span> <span class="hljs-attr">defaultValue</span>=<span class="hljs-string">&#123;0&#125;</span> /&gt;</span></span><br><span class="language-xml">        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&#123;this.endRef&#125;</span> <span class="hljs-attr">defaultValue</span>=<span class="hljs-string">&#123;10&#125;</span> /&gt;</span></span><br><span class="language-xml">        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;primary&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">&#123;this.clip&#125;</span>&gt;</span>clip<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span><br><span class="language-xml">        <span class="hljs-tag">&lt;<span class="hljs-name">audio</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&#123;this.audioRef&#125;</span> <span class="hljs-attr">controls</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;/song.mp3&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">audio</span>&gt;</span></span><br><span class="language-xml">        <span class="hljs-tag">&lt;<span class="hljs-name">audio</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&#123;this.audioClipRef&#125;</span> <span class="hljs-attr">controls</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">audio</span>&gt;</span></span><br><span class="language-xml">      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span><br>    )<br>  &#125;<br>&#125;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">createWorker</span>(<span class="hljs-params">workerPath</span>) &#123;<br>  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(workerPath);<br>&#125;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">getClipCommand</span>(<span class="hljs-params">arrayBuffer, start = <span class="hljs-number">0</span>, duration = <span class="hljs-number">10</span></span>) &#123;<br>  <span class="hljs-keyword">return</span> &#123;<br>    <span class="hljs-attr">type</span>: <span class="hljs-string">&quot;run&quot;</span>,<br>    <span class="hljs-attr">arguments</span>: <span class="hljs-string">`-ss <span class="hljs-subst">$&#123;start&#125;</span> -i input.mp3 <span class="hljs-subst">$&#123;</span></span><br><span class="hljs-subst"><span class="hljs-string">      duration ? <span class="hljs-string">`-t <span class="hljs-subst">$&#123;duration&#125;</span> `</span> : <span class="hljs-string">&quot;&quot;</span></span></span><br><span class="hljs-subst"><span class="hljs-string">      &#125;</span>-acodec copy output.mp3`</span>.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot; &quot;</span>),<br>    <span class="hljs-attr">MEMFS</span>: [<br>      &#123;<br>        <span class="hljs-attr">data</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(arrayBuffer),<br>        <span class="hljs-attr">name</span>: <span class="hljs-string">&quot;input.mp3&quot;</span><br>      &#125;<br>    ]<br>  &#125;;<br>&#125;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">toPromise</span>(<span class="hljs-params">worker, info</span>) &#123;<br>  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> &#123;<br>    <span class="hljs-keyword">const</span> onSuccess = <span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) &#123;<br>      <span class="hljs-keyword">switch</span> (event.<span class="hljs-property">data</span>.<span class="hljs-property">type</span>) &#123;<br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;done&quot;</span>:<br>          worker.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">&quot;message&quot;</span>, onSuccess);<br>          <span class="hljs-title function_">resolve</span>(event);<br>          <span class="hljs-keyword">break</span>;<br>        <span class="hljs-attr">default</span>:<br>          <span class="hljs-keyword">break</span>;<br>      &#125;<br>    &#125;;<br>    worker.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">&quot;message&quot;</span>, onSuccess);<br>    info &amp;&amp; worker.<span class="hljs-title function_">postMessage</span>(info);<br>  &#125;);<br>&#125;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">audioBufferToBlob</span>(<span class="hljs-params">arrayBuffer</span>) &#123;<br>  <span class="hljs-keyword">const</span> file = <span class="hljs-keyword">new</span> <span class="hljs-title class_">File</span>([arrayBuffer], <span class="hljs-string">&#x27;test.mp3&#x27;</span>, &#123;<br>    <span class="hljs-attr">type</span>: <span class="hljs-string">&#x27;audio/mp3&#x27;</span>,<br>  &#125;);<br>  <span class="hljs-keyword">return</span> file;<br>&#125;<br><br></code></pre></td></tr></table></div></figure>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/" class="category-chain-item">前端基础</a>
  
  
    <span>></span>
    
  <a href="/categories/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/JS/" class="category-chain-item">JS</a>
  
  
    <span>></span>
    
  <a href="/categories/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/JS/%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2/" class="category-chain-item">进制转换</a>
  
  

  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/">#前端基础</a>
      
        <a href="/tags/js/">#js</a>
      
        <a href="/tags/%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AF%B9%E8%B1%A1/">#二进制对象</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>二进制对象</div>
      <div>http://81.68.132.127/2022/10/20/JS/binary/二进制对象/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Ikki</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年10月20日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2022年10月20日</div>
        </div>
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/10/23/dev_ops/linux/linux%E5%9F%BA%E7%A1%80%E5%91%BD%E4%BB%A4/" title="01.linux基础命令">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">01.linux基础命令</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/10/20/JS/binary/IEEE754%E6%A0%87%E5%87%86/" title="IEEE754标准">
                        <span class="hidden-mobile">IEEE754标准</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  <article id="comments" lazyload>
    
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"6KzYWc2oaEBaHgNl4UIicMJC-gzGzoHsz","appKey":"vJ4FIwKeHRmHraCJraAsjyZ7","path":"window.location.pathname","placeholder":"请输入你的评论\n昵称为必填项目(输入QQ号码可以自动获取昵称和邮箱)\n虽然email不是必选，但是填写了email可以收到推送通知哦！","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":true,"recordIP":true,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":true},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


  </article>


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
      IKKI-专业BUG开发
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="/fluid/js/star.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
